package lingou.homework.postbar.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lingou.homework.postbar.dao.IdentityMapper;
import lingou.homework.postbar.dao.UserMapper;
import lingou.homework.postbar.domain.Identity;
import lingou.homework.postbar.domain.User;
import lingou.homework.postbar.exception.BusinessException;
import lingou.homework.postbar.exception.SystemException;
import lingou.homework.postbar.service.enums.UserTypeEnum;
import lingou.homework.postbar.service.tool.Tools;
import lingou.homework.postbar.tool.StaticURL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    IdentityMapper identityMapper;
    private User getUserByEmail(String email){
        return userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getEmail, email));
    }
    public boolean isManager(long userId){
        return UserTypeEnum.select(getUser(userId)) == UserTypeEnum.MANAGER;
    }
    public User login(String email, String password){
        User user = getUserByEmail(email);
        if(user != null && user.getPassword().equals(password)) return user;
        return null;
    }
    public void register(String username, String email, String password){
        User user = new User();
        user.setEmail(email);
        user.setUserName(username);
        user.setUserType(0L);
        user.setStatus(0L);
        user.setPassword(password);
        user.setTime(Tools.getNow());
        userMapper.insert(user);
    }
    public boolean exists(String email){
        return getUserByEmail(email) != null;
    }
    public List<User> getAllUsers(){
        return userMapper.selectList(null).stream()
                .filter(user -> user.getStatus() == 0).toList();
    }
    public User getUser(long userId){
        User user = userMapper.selectById(userId);
        if(user == null) throw new BusinessException("用户不存在！");
        return user;
    }
    public void updateUser(User user){
        userMapper.updateById(user);
    }

    public List<Identity> getUnidentified() {
        return identityMapper.selectList(new QueryWrapper<Identity>()
                .eq("status", 0L));
    }

    @Transactional
    public void identifyPass(long managerId, long userId) {
        if(!isManager(managerId)) throw BusinessException.NO_PERMISSION;
        Identity identity = identityMapper.selectById(userId);
        identity.setStatus(1L);
        identityMapper.updateById(identity);
    }

    public void identifyRequest(long userId, String name, String studentId, MultipartFile image) {
        Identity identity = identityMapper.selectById(userId);
        if(identity == null){
            identity = new Identity();
            identity.setUserId(userId);
            identity.setName(name);
            identity.setStudentId(studentId);
            identityMapper.insert(identity);
        }
        else {
            identity.setName(name);
            identity.setStudentId(studentId);
            identity.setStatus(0L);
            identityMapper.updateById(identity);
        }
        try {
            StaticURL.saveToPool(image.getInputStream(), StaticURL.IDENTIFY_POOL, String.valueOf(userId));
        } catch (IOException e) {
            throw SystemException.FAILED;
        }
    }

    public Identity getIdentity(Long userId){
        if(userId == null || userId < 0) return null;
        Identity identity = identityMapper.selectById(userId);
        if(identity == null || identity.getStatus() == 0L) return null;
        return identity;
    }
}
